package day1027.dem3.one;

import javax.jws.Oneway;
import java.lang.reflect.Field;

/**
 * @program: fanxing
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-10-27 16:32
 **/
public class Test {
    public static void main(String[] args) throws Exception {
        Class<Pig> pigClass = Pig.class;
        Pig pig = pigClass.newInstance();
        System.out.println(pig);
//获取反射类对应的属性对象
        Field name =pigClass.getDeclaredField("name");
        //为属性赋值-- pig.setName(); 普通的赋值
        name.set(pig,"佩奇");
//通过反射默认对指定属性的对象赋值
        System.out.println(pig);
 //通过反射获取指定属性的对象值。  pig.getName()普通获取
        Object o = name.get(pig);
        System.out.println(o);
        //因为age是一个私有属性，无法直接为私有属性赋值。
        Field age = pigClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(pig,3);
        System.out.println(pig);
        Object o1 = age.get(pig);
        System.out.println(o1);
    }
}
class Zoo{
    public String address;
    private String sex;
}
class Pig extends Zoo{
    public String name;
    private Integer age;


    @Override
    public String toString() {
        return "Pig{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

